/*
 * 文 件 名:  JsoupParseTest.java
 * 版    权:   深圳埃思欧纳信息咨询有限公司版权所有. YYYY-YYYY,  All rights reserved
 * 描    述:   <描述>
 * 修 改 人:  honny.huang
 * 修改时间: 2011-3-16
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.jsoup;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;


/**
 * <一句话功能简述> <功能详细描述>
 *
 * @author honny.huang
 * @version [版本号, 2011-3-16]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */

public class JsoupParseTest 
{
	private String swfUrl;

	/**
	 * <解析优酷视频地址页面>
	 * 
	 * @param sqlDataSource
	 *            参数集
	 * @return []
	 * 
	 * @return Map [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String jsoupParseYouKuUrl(String path)
	{
		String resultStr = "";
		try 
		{
			URL url = new URL(path);   
			Document doc = Jsoup.parse(url, 1000 * 2);  
			if (null != doc && doc.data().length()> 0) 
			{
				Element titleEle = doc.getElementsByTag("title").first();
				String titleStr = titleEle.text().toString();
				Element imgEle = doc.getElementsByAttributeValue("id", "download").first();
				String imgStr = "";
				if (imgEle.tagName().equals("a")) 
				{
				   imgStr = imgEle.attr("href");
				   imgStr = imgStr.substring(imgStr.indexOf("http") + 4);
//				   imgStr = imgStr.substring(imgStr.indexOf("http"), imgStr.indexOf("|\""));
				   imgStr = imgStr.substring(imgStr.indexOf("http"), imgStr.lastIndexOf("|"));
				}
				Element playUrlEle = doc.getElementsByAttributeValue("id", "link2").first();
				String playUrlStr = "";
				if (playUrlEle.tagName().equals("input"))
				{
					playUrlStr = playUrlEle.val();
				}
				StringBuffer sb = new StringBuffer();
				sb.append("<videoinfo TotalRecords='1'>");
				sb.append("<Row>");
				sb.append("<videoTitle>");// 视频标题
				sb.append("<![CDATA[" + titleStr + "]]>");
				sb.append("</videoTitle>");
				sb.append("<videoUrl>");// 视频url
				sb.append("<![CDATA[" + playUrlStr + "]]>");
				sb.append("</videoUrl>");
				sb.append("<videoPhoto>");// 视频封面
				sb.append("<![CDATA[" + imgStr + "]]>");  // http://g4.ykimg.com/01270F1F464D814B13C28100000000BB98CCE3-AC51-EF1E-37EF-20BE703EE097
				sb.append("</videoPhoto>");
				sb.append("</Row>");
				sb.append("</videoinfo>");
				resultStr = sb.toString();
			}
			else 
			{
				resultStr = getOldUrl(path);
			}
		} 
		catch (MalformedURLException e) //如果字符串指定未知协议
        {   
            e.printStackTrace();   
        } catch (IOException e) 
        {   
            e.printStackTrace();   
        }   
		return resultStr;
	}
	
	/**
	 * <解析56视频地址页面>
	 * 
	 * @param sqlDataSource
	 *            参数集
	 * @return []
	 * 
	 * @return Map [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	public String jsoupParse56Url(String path)
	{
		String resultStr = "";
		try 
		{
			URL url = new URL(path);   
			Document doc = Jsoup.parse(url, 1000 * 2);  
			if (null != doc && doc.data().length()> 0) 
			{
				Element titleEle = doc.getElementsByTag("title").first();
				String titleStr = titleEle.text().toString();
				Element imgEle = doc.getElementsByAttributeValue("id", "download").first();
				String imgStr = "";
				if (imgEle.tagName().equals("a")) 
				{
				   imgStr = imgEle.attr("href");
				   imgStr = imgStr.substring(imgStr.indexOf("http") + 4);
//				   imgStr = imgStr.substring(imgStr.indexOf("http"), imgStr.indexOf("|\""));
				   imgStr = imgStr.substring(imgStr.indexOf("http"), imgStr.lastIndexOf("|"));
				}
				Element playUrlEle = doc.getElementsByAttributeValue("id", "link2").first();
				String playUrlStr = "";
				if (playUrlEle.tagName().equals("input"))
				{
					playUrlStr = playUrlEle.val();
				}
				StringBuffer sb = new StringBuffer();
				sb.append("<videoinfo TotalRecords='1'>");
				sb.append("<Row>");
				sb.append("<videoTitle>");// 视频标题
				sb.append("<![CDATA[" + titleStr + "]]>");
				sb.append("</videoTitle>");
				sb.append("<videoUrl>");// 视频url
				sb.append("<![CDATA[" + playUrlStr + "]]>");
				sb.append("</videoUrl>");
				sb.append("<videoPhoto>");// 视频封面
				sb.append("<![CDATA[" + imgStr + "]]>");
				sb.append("</videoPhoto>");
				sb.append("</Row>");
				sb.append("</videoinfo>");
				resultStr = sb.toString();
			}
			else 
			{
				resultStr = getOldUrl(path);
			}
		} 
		catch (MalformedURLException e) //如果字符串指定未知协议
        {   
            e.printStackTrace();   
        } catch (IOException e) 
        {   
            e.printStackTrace();   
        }   
		return resultStr;
	}
	
	/**
	 * <获取原视频地址>
	 * 
	 * @param sqlDataSource
	 *            参数集
	 * @return []
	 * 
	 * @return Map [返回类型说明]
	 * @exception throws [违例类型] [违例说明]
	 * @see [类、类#方法、类#成员]
	 */
	private String getOldUrl(String url) 
	{
		StringBuffer sb = new StringBuffer("");
		sb.append("<videoinfo TotalRecords='1'>");
		sb.append("<Row>");
		sb.append("<videoUrl>");// 视频url
		sb.append("<![CDATA[" + url + "]]>");
		sb.append("</videoUrl>");
		sb.append("</Row>");
		sb.append("</videoinfo>");
		return sb.toString();
	}

	public String getSwfUrl() 
	{
		return swfUrl;
	}

	public void setSwfUrl(String swfUrl) 
	{
		this.swfUrl = swfUrl;
	}



	 public static void main(String[] args) 
	 {   
		 JsoupParseTest parseTest = new JsoupParseTest();   
         String url = "http://v.youku.com/v_playlist/f5618298o1p0.html";
//         String url = "http://www.56.com/p21/v_MTAwMjM5NTc4.html";
         String resultStr = "";
         if (url.indexOf("v.youku.com") != -1) 
         {
 			resultStr = parseTest.jsoupParseYouKuUrl(url);
 		 }
         System.out.println("=============");
         System.out.println(resultStr);
         System.out.println("=============");
	 }   
}
